package com.wsd.smartcarwasher.controller;


import com.wsd.smartcarwasher.configuration.GetSessionConfig;
import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.dao.PriceTimeMapper;
import com.wsd.smartcarwasher.dao.StatisticDao;
import com.wsd.smartcarwasher.domain.Partner;
import com.wsd.smartcarwasher.domain.PriceTime;
import com.wsd.smartcarwasher.domain.PriceTimeExample;
import com.wsd.smartcarwasher.service.MachineService;
import com.wsd.smartcarwasher.service.PartnerService;
import com.wsd.smartcarwasher.service.PriceTimeService;
import com.wsd.smartcarwasher.service.WxService;
import com.wsd.smartcarwasher.util.ReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "(新增)价格时段配置")
@Controller
@CrossOrigin
public class PriceTimeController {

    @Resource
    private PriceTimeService priceTimeService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private WxService wxService;
    @Resource
    private PartnerService partnerService;
    @Resource
    private PriceTimeMapper priceTimeMapper;
    @Resource
    @Lazy
    private MachineService machineService;
    @Resource
    @Lazy
    private StatisticDao statisticDao;

    @RequestMapping(
            value = {"/v1/getPriceTime"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getPriceTime(@RequestParam int machineId) {

        return ReturnUtil.Success("查询成功",this.priceTimeService.queryPriceTimeAll(machineId).get(0).getType());
    }
    /**
     * 小程序
     * @param machineId
     * @return
     */
    @RequestMapping(
            value = {"/v1/store/getPriceTime"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getStorePriceTime(@RequestParam int machineId) {
        return ReturnUtil.Success("查询成功",this.priceTimeService.queryStorePriceTime(machineId).isEmpty()?null:this.priceTimeService.queryStorePriceTime(machineId));

    }
    //管理端
    @ApiOperation(
            value = "pc端-获取商家端价格",
            notes = "pc端-获取商家端价格",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/partner/getPriceTime"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getPartnerPriceTime(@RequestParam int machineId) {

        return ReturnUtil.Success("查询成功",this.priceTimeService.queryPartnerPriceTimeAll(machineId));
    }
    @ApiOperation(
            value = "pc端-获取商家端价格状态",
            notes = "pc端-获取商家端价格状态",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/partner/getPriceTimeStatus"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getPriceTimeStatus(@RequestParam int machineId) {
        PriceTime changes=this.priceTimeService.queryPriceTimeStatus(machineId);
        if(changes==null)
        {
            return ReturnUtil.Success("查询成功",null);
        }
        return ReturnUtil.Success("查询成功",changes);
    }


    //是否遵循商家提交的价格设定
    @ApiOperation(
            value = "pc端-是否遵循商家提交的价格设定",
            notes = "pc端-是否遵循商家提交的价格设定",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/partner/setPriceTimeStatus"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap isObeyPartner(@RequestParam(value = "partnerId")Integer partnerId,
                                  @RequestParam(value = "status")byte status,
                                  @RequestParam(value = "machineId")Integer machineId,
                                    @RequestParam(value="priceId") Integer priceTimeId) {
        PriceTimeExample priceTimeExample=new PriceTimeExample();
        priceTimeExample.createCriteria().andPartnerIdIsNotNull()
                .andIsplatFormEqualTo((byte)1)
                .andMachineIdEqualTo(machineId)
                .andStatusEqualTo((byte)1);
        List<PriceTime> priceTimeList=this.priceTimeMapper.selectByExample(priceTimeExample);
        if(!priceTimeList.isEmpty())
        {
            return  ReturnUtil.Error("已存在审核通过的商家价格");
        }
        int ls =priceTimeService.updateIsObeyPartner(status,partnerId,machineId,priceTimeId);
        if(ls==0){
            return ReturnUtil.Error("设置失败");
        }
        return ReturnUtil.Success("设置成功");
    }

    //管理端设置商家价格
    @ApiOperation(
            value = "pc端-设置商家价格",
            notes = "pc端-设置商家价格",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/partner/setPriceTime"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap setPartnerPriceTime(@RequestBody PriceTime priceTime) {

        if(this.priceTimeService.isStorePriceTime(priceTime.getMachineId()).isEmpty())
        {

            int ls =priceTimeService.addPriceTime(priceTime);
            if(ls==0){
                return ReturnUtil.Error("添加失败");
            }
            return ReturnUtil.Success("添加成功",priceTime);
        }else{

            int ls =priceTimeService.updatePartnerPriceTime(priceTime);
            if(ls==0){
                return ReturnUtil.Error("修改失败");
            }
            return ReturnUtil.Success("修改成功",priceTime);
        }

    }





    @RequestMapping(
            value = {"/v1/setPriceTime"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap setPriceTime(@RequestBody PriceTime priceTime) {

            int ls =priceTimeService.updatePriceTime(priceTime);

            if(ls==0){

                return ReturnUtil.Error("修改失败");
            }

            return ReturnUtil.Success("修改成功");
    }

    @RequestMapping(
            value = {"/v1/store/setPriceTime"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap setStorePriceTime(@RequestBody PriceTime priceTime) {

            int ls =priceTimeService.updateStorePriceTime(priceTime);
            if(ls==0){
                return ReturnUtil.Error("修改失败");
            }
            return ReturnUtil.Success("修改成功");
        }

    /**
     * 一键设置所有机器时段
     * @param priceTime
     * @return
     */
    @RequestMapping(
            value = {"/v1/set/allPriceTime"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap allPriceTime(@RequestBody PriceTime priceTime) {

            int ls =priceTimeService.setAllPriceTime(priceTime);
            if(ls==0){
                return ReturnUtil.Error("修改失败");
            }
            return ReturnUtil.Success("修改成功");
        }
    @ApiOperation(value = "商家端新增、修改价格时段")
    @RequestMapping(
            value = {"/v1/store/updatePriceTime"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap updateStorePrice(@RequestBody PriceTime priceTime) {

String sessionId = request.getHeader("authorization");
        String openId = this.wxService.getOpenId(sessionId);
//String openId="o1MBY6MMA3D1NM_WuA8OuuwrgxrM";
//        String openId = "o1MBY6B7r8YCuaKXdxOZM_uE_EHI";
        if (StringUtils.isBlank(openId)) {
            return ReturnUtil.Error(ResultState.LOGIN_AGAIN.getMsg());
        } else {
            Partner partner = this.partnerService.getPartnerByOpenId(openId);
            if (partner == null) {
                return ReturnUtil.Error(ResultState.NO_BIND_PARTNER.getMsg());
            } else {
                if(partner.getIs_dataer()==1)
                {
                    return ReturnUtil.Error("没有权限");
                }
            priceTime.setPartnerId(partner.getId());
            priceTime.setTimeName("0");
            priceTime.setType(this.priceTimeService.covertPriceTimetoString(priceTime.getPriceTimeModelList()));

           if(this.priceTimeService.isStorePriceTime(priceTime.getMachineId()).isEmpty())
           {
//               if(priceTime.getIsplatForm()==0)
//               {
//                   priceTime.setStatus(1);
//                   int ls =priceTimeService.updatePriceTime(priceTime);
//                   return ReturnUtil.Success("服从平台设置成功");
//               }
               priceTime.setStatus(1);
               priceTime.setIsplatForm((byte)1);
               int ls =priceTimeService.addPriceTime(priceTime);
               if(ls==0){
                   return ReturnUtil.Error("添加失败");
               }
               return ReturnUtil.Success("添加成功",priceTime);
           }else{
               priceTime.setStatus(1);
               priceTime.setIsplatForm((byte)1);
               int ls =priceTimeService.updateStorePriceTime(priceTime);
               if(ls==0){
                   return ReturnUtil.Error("新增失败");
               }
               return ReturnUtil.Success("新增成功",priceTime);
           }
    }} }

    @RequestMapping(
            value = {"/v1/deletePriceTime"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap deletePrice(@RequestParam int priceId) {
//        priceTime.setCreate_time(new Date());

        PriceTime priceTime=this.priceTimeService.getPrice(priceId);
        priceTime.setStatus(1);
            int ls =priceTimeService.updatePriceTime(priceTime);
            return ls==1?ReturnUtil.Success("成功"):ReturnUtil.Error("失败");

    }

    @RequestMapping(
            value = {"/v1/store/deletePriceTime"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap storeDeletePrice(@RequestParam int priceId) {
        GetSessionConfig config=new GetSessionConfig();
        Map<String,String> map= config.getStorePartnerId("删除价格时段",request);
        PriceTime priceTime=this.priceTimeService.getPrice(priceId);
        priceTime.setStatus(1);
        int ls =priceTimeService.updatePriceTime(priceTime);
        return ls==1?ReturnUtil.Success("成功"):ReturnUtil.Error("失败");
    }

    @ApiOperation(value = "商家端更改价格通知")
    @RequestMapping(
            value = {"/v1/getPriceTimeMessageList"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getPriceTimeMessageList() {

        PriceTimeExample priceTimeExample=new PriceTimeExample();
        priceTimeExample.createCriteria().andIsplatFormEqualTo((byte)1)
                .andPartnerIdIsNotNull();
        List<PriceTime> priceTimeList= this.priceTimeMapper.selectByExample(priceTimeExample);
//        List<PriceTimeReuse> priceTimeList=this.statisticDao.selectPriceMessage();
        List<PriceTime> priceTimes_Read=new ArrayList<>();
        System.out.println(priceTimeList);
        int flag=0;
        for (PriceTime p:priceTimeList
             ) {
            p.setMachineName(this.machineService.queryMachineById(p.getMachineId()).getName());
            p.setPartnerName(this.partnerService.getPartnerById(p.getPartnerId()).getRealname());

            priceTimes_Read.add(p);
            p.setTimeName("1");
//            p.setCreate_time(p.getCreate_time());
            this.priceTimeMapper.updateByPrimaryKey(p);

        }
        Map<String,Object> map=new HashMap<>();
        return  ReturnUtil.Success("",priceTimes_Read);
    }

    @ApiOperation(value = "商家端更改价格列表")
    @RequestMapping(
            value = {"/v1/partnersPriceTimeList"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap partnersPriceTimeList() {
        PriceTimeExample priceTimeExample=new PriceTimeExample();
        priceTimeExample.createCriteria().andIsplatFormEqualTo((byte)1)
                .andPartnerIdIsNotNull();
        List<PriceTime> priceTimeList= this.priceTimeMapper.selectByExample(priceTimeExample);
        int flag=0;
        for (PriceTime p:priceTimeList
        ) {
            p.setMachineName(this.machineService.queryMachineById(p.getMachineId()).getName());
            p.setPartnerName(this.partnerService.getPartnerById(p.getPartnerId()).getRealname());
        }
        return  ReturnUtil.Success("",priceTimeList);
    }

}
